<%--
  Created by IntelliJ IDEA.
  User: 施明
  Date: 2019/5/21
  Time: 20:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<c:set var="path" value="${pageContext.request.contextPath}"></c:set>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${path}/layuiadmin/layui/css/layui.css" media="all">
     <link rel="stylesheet" href="${path}/layuiadmin/style/admin.css" media="all">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>

<body>
<div id="editPictureForm" style="display: none">
    <ins class="adsbygoogle"
         style="display: inline-block; width: 300px; height: 15px"
         data-ad-client="ca-pub-6111334333458862" data-ad-slot="3820120620"></ins>
    <form class="layui-form" action="" lay-filter="example">

        <div class="layui-inline">
            <label class="layui-form-label">图片id</label>
            <div class="layui-input-inline">
                <input type="text" name="pictureid" id="pictureid" readonly="readonly"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">商品名</label>
            <div class="layui-input-inline">
                <input type="text" name="goodsname" id="goodsname" readonly="readonly"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="ifcoverbtn">重新上传图片</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="ifcover">
                <p id="ifcoverText"></p>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="editGoodspicture">修改图片</button>
            </div>
        </div>
    </form>
</div>

<div class="demoTable">
    商品名：
    <div class="layui-inline">
        <input class="layui-input" name="goodsname" id="demoReload"
               autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>
</div>


<table class="layui-hide" id="test" lay-filter="test" ></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="addPicture">添加图片</button>
    </div>
</script>


<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="editPicture">修改图片</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="${path}/layuiadmin/layui/layui.js"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<style type="text/css">

    .layui-table-cell {
        height: auto;
        line-height: 28px;
    }
</style>
<script>


    layui.use(['table','upload','form'], function() {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery
            ,upload = layui.upload;
        var cover;


        table.render({
            elem: '#test'
            ,
            url: 'listGoodsPicture'
            ,
            toolbar: '#toolbarDemo',
            title: '用户数据表',

            cols: [
                [
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'pictureid', width: 100, title: '图片ID', sort: true },
                    { field: 'goodsid', width: 100, title: '商品ID', sort: true,hide:true},
                    { field: 'goodsname', width: 100, title: '商品名', sort: true},
                    { field: 'pictureurl', width: 100,hight:500, title: '图片', sort: true },
                    { field: 'ifcover', width: 100, title: '是否封面', sort: true,templet : function (res) {
            return res.ifcover == 0 ? "是" : "否";
        } },

                    { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
                ]
            ],
            page: true
            , id: 'testReload'
        });

//头工具栏事件
        table.on('toolbar(test)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;
                case 'addPicture':
                    window.location.href="addPicture";
                    break;

            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function(obj) {
            var data = obj.data;

            if(obj.event === 'del') {
                console.log(data.goodsid);
                    $.ajax({
                        type: "get",
                        url: "delGoodsPicture",
                        data:{
                            "goodsid":data.goodsid,
                        },
                        success: function (data) {
                            if (data.msg == "删除成功") {
                                layer.msg(data.msg, {
                                    time: 1000
                                }, function () {
                                   // layer.close(index);
                                    location.reload();
                                });
                            } else {
                                layer.msg(data.msg)
                            }
                        }

                  //  layer.close(index);
                });
            } else if(obj.event === 'editPicture') {
                $("#pictureid").attr("value", data.pictureid);
                $("#goodsname").attr("value", data.goodsname);
                //$("#goodssalenum").attr("value", data.pictureurl);
                var index1 = layer.open({
                        title: "修改库存",
                        skin: 'layui-layer-rim',
                        type: 1,
                    area:["700px","700px"],
                    content: $("#editPictureForm"),
                });
                //只有加了这一句，表单的复选框，单选框才可以编辑
                layui.form.render();
                upload.render();

                //普通图片上传
                var uploadInst1 = upload.render({
                    elem: '#ifcoverbtn'
                    ,url: 'uploadPicture'
                    ,before: function(obj){
                        //预读本地文件示例，不支持ie8
                        obj.preview(function(index, file, result){
                            $('#ifcover').attr('src', result); //图片链接（base64）

                        });
                    }
                    ,done: function(res){
                        //如果上传失败
                        cover=res.data.src;
                        style:'display:inline-block;max-width:20%;height:auto';

                        if(res.code > 0){
                            return layer.msg('上传失败');
                        }
                        //上传成功
                    }
                    ,error: function(){
                        //演示失败状态，并实现重传
                        var ifcoverText = $('#ifcoverText');
                        ifcoverText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                        ifcoverText.find('.demo-reload').on('click', function(){
                            uploadInst.upload();
                        });
                    }
                });
                layui.form.render();
                upload.render();

                //监听提交
                layui.form.on('submit(editGoodspicture)', function (data2) {
                    var d = data2.field;
                    console.log(d)
                       $.ajax({
                            type: "get",
                            url: "editGoodsPicture",
                            data:{
                                "pictureid":d.pictureid,
                                "picuture":cover,
                            },
                            success: function (data) {
                                if (data.msg == "修改成功") {
                                    layer.msg(data.msg, {
                                        time: 1000
                                    }, function () {
                                        layer.close(index1);
                                        location.reload();
                                    });
                                } else {
                                    layer.msg(data.msg)
                                }
                            }
                        });

                    return false;
                });
            }
        });
        //模糊查询
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1
                        //重新从第 1 页开始
                    },
                    where: {
                        content: demoReload.val(),
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>

</html>